/*
 * Copyright by Pietro Lusso 2014
 */

/**
 * FM global Finalministry Functionality
 */
var FM = {};

/*
 * Fullscreen mode
 */
FM.toggleFullScreen = function() {
	if (!document.fullscreenElement && // alternative standard method
	!document.mozFullScreenElement && !document.webkitFullscreenElement
			&& !document.msFullscreenElement) { // current working methods
		if (document.documentElement.requestFullscreen) {
			document.documentElement.requestFullscreen();
		} else if (document.documentElement.msRequestFullscreen) {
			document.documentElement.msRequestFullscreen();
		} else if (document.documentElement.mozRequestFullScreen) {
			document.documentElement.mozRequestFullScreen();
		} else if (document.documentElement.webkitRequestFullscreen) {
			document.documentElement
					.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
		}
	} else {
		if (document.exitFullscreen) {
			document.exitFullscreen();
		} else if (document.msExitFullscreen) {
			document.msExitFullscreen();
		} else if (document.mozCancelFullScreen) {
			document.mozCancelFullScreen();
		} else if (document.webkitExitFullscreen) {
			document.webkitExitFullscreen();
		}
	}
};



/**
 * Initialize the dropDown List of the Congregation on the index page
 */
FM.initCongregationDropDownList = function() {
	$.getJSON(
					"/congregation/all",
					function(json) {

						var list = "<li><a class=\"btn btn-primary btn-lg\" style=\" border-radius: 0px;\" href=\"#\">Add a new one ...</a></li>";

						$.each(json, function(index, congregation) {

							list += "<li><a href=\"#\">" + congregation.name
									+ "</a></li>";
						});

						$('#congregationList').html(list);

					});
};

/*
 * Init
 */
FM.init = function() {
	console.log("INIT Finalministry JavaScript Functionality");
	// Enhance elements with click functionality
	$(document).ready(
			function() {

				// BUTTONS with a path
				$("button").click(
						function(event) {
							if ($(event.target).attr("path") != null) {
								$("#fmContent").html("<img src=\"finalministry/images/720.gif\"/>");
								$("#fmContent").load(
										$(event.target).attr("path")
												.toLowerCase());
							}
						});

				// HYPERLINKS with a path
				$("a").click(
						function(event) {

							var link = event.target;

							if ($(link).attr("path") != null) {
								$("#fmContent").html("<img src=\"finalministry/images/720.gif\"/>");
								$("#fmContent").load(
										$(link).attr("path").toLowerCase());
							}
						});
				
				FM.initCongregationDropDownList();
			});
};

/**
 * Ask a question which can be answered only with yes or no FIXME: please,
 * problems with JQuery UI !
 * 
 * @param digitalQuestion
 * @returns true or false boolean
 */
FM.showAskSimpleQuestionDialog = function(title, digitalQuestion) {
	var dialog = document.createElement("div");
	dialog.setAttribute("id", "simpleQuestionDialog");
	dialog.setAttribute("title", title);
	var content = document.createElement("p");
	dialog.appendChild(content);
	content.innerHTML = digitalQuestion;

	document.getElementsByTagName("body")[0].appendChild(dialog);

	$("#simpleQuestionDialog").dialog({
		modal : true,
		buttons : {
			Yes : function() {
				$(this).dialog("close");
				$('#fmContent').load('shutdown');
				setTimeout(gotoPayPal, 300);
			},
			No : function() {
				$(this).dialog("close");
				return false;
			}
		}
	});
};

/**
 * Initialize the contact list for injecting edit functionality
 */
FM.initContacts = function() {

	var contactManager = {};

	contactManager.showEditTools = function(row) {

		var $tds = row.find("td");

		$.each($tds, function() { // Visits every single <td> element
			console.log($(this).text()); // Prints out the text within the
			// <td>
		});

		$('#contactsDiv').load('/contacts/editdialog/1', function() {
			$('#myModal').modal('show');
		});
	}

	$('.contact').dblclick(function() {

		contactManager.showEditTools(jQuery(this));
	});
	
	FM.loadComponent("contactEditTools");
};

/**
 * Appends a component to the dynamic content.
 */
FM.loadComponent = function(componentName) {
	$.get( "finalministry/components/" + componentName + ".htm", function( data ) {
		
		$("#fmContent").append(data);
	});
	
};

function gotoPayPal() {
	window.location.replace("http://pietro-lusso.de");
}
